<?php

/**
 * Celebrio nette-bootstrap file.
 *
 */
use Nette\Debug;
use Nette\Environment;
use Nette\Config\Config;
use Nette\Application\Route;
use Nette\Application\SimpleRouter;
use \Microsoft_WindowsAzure_Storage_Table;
use \Microsoft_WindowsAzure_SessionHandler;
use \MultipleFileUpload;
use Celebrio\Core\ModuleLoader;

// Step 1: Load Nette Framework
// this allows load Nette Framework classes automatically so that
// you don't have to litter your code with 'require' statements
require LIBS_DIR . '/Nette/loader.php';

// sets a tempDir to TempStorage for Windows Azure using local azure storage
if (extension_loaded("azure")) {
    $azureTempDir = azure_getlocalresourcepath("TempStorage");
    Environment::setVariable("tempDir", substr($azureTempDir, 0, -1));
}

// load configuration from config.ini file
Environment::loadConfig();

// TODO: this type of loging is not usable for distributed environment in cloud
// sets a logDir to LogStorage for Windows Azure using local azure storage
if (extension_loaded("azure")) {
    $azureLogDir = azure_getlocalresourcepath("LogStorage");
    Environment::setVariable("logDir", substr($azureLogDir, 0, -1));
}

if (extension_loaded("azure"))
    ini_set('error_log', azure_getlocalresourcepath("ErrorStorage") . 'error_log.txt');

// development mode - errors are displayed
Debug::enable();

//production environment - errors are logged
//$logPHPErr = Environment::expand('%logPHPErr%');
//Debug::enable(Debug::PRODUCTION, "%logDir%/$logPHPErr", 'admin@example.com');


Debug::$strictMode = TRUE;

if (extension_loaded("azure")) {
    // stores session in azure storage tables using php_azure.dll, php atable.core.windows.net'zure SDK and configurations in ServiceConfiguration.csdef
    $storageClient = new Microsoft_WindowsAzure_Storage_Table("table.core.windows.net", azure_getconfig("StorageAccountName"), azure_getconfig("StorageAccountKey"));
    $sessionHandler = new Microsoft_WindowsAzure_SessionHandler($storageClient, 'sessionstable');
    $sessionHandler->register();
} else {
    // stores sessions in temp directory
    ini_set('session.save_path', Environment::expand("%tempDir%"));
}

// watching performance on Windows Azure
//if(extension_loaded("azure")) DiagnosticsManagement::addPerformanceCounter('\Processor(*)\% Processor Time');
// Step 3: Configure application
// 3a) get and setup a front controller
$application = Environment::getApplication();
//$application->catchExceptions = false;

$application->errorPresenter = 'Kernel:Error';
//$application->catchExceptions = TRUE;


Route::addStyle("#kernel");
Route::setStyleProperty("#kernel", Route::FILTER_TABLE, array(
    'sign-in' => 'SignIn',
    'overview' => 'Overview',
    'sign-out' => 'SignOut',
    'php-logs' => 'Logs:Php:Display',
    'acl-logs' => 'Logs:Acl:Display',
    'php-logs-process' => 'Logs:Php:Process',
    'logs' => 'Logs:Overview',
    'modules' => 'Modules:ModulesManagement',
    'console' => 'Modules:Console',
    'terminal' => 'Modules:Terminal',
    'google-api' => 'Modules:GoogleApi',
    'picasa' => 'Modules:Picasa',
    'acl' => 'Acl:Overview',
    'permissions' => 'Acl:Permissions',
    'privileges' => 'Acl:Privileges',
    'resources' => 'Acl:Resources',
    'user-management' => 'Users:Overview',
    'users' => 'Users:Users',
    'roles' => 'Users:Roles',
));

// webtop translation table
Route::addStyle("#webtop");
Route::setStyleProperty("#webtop", Route::FILTER_TABLE, array(
    'default' => 'Default'
));

Route::addStyle("#applications");
Route::setStyleProperty("#applications", Route::FILTER_TABLE, array(
    'preferences' => 'Preferences:Preferences',
    //'desktop-preferences' => 'SystemApp:Preferences:Desktop',
    //'explorer' => 'SystemApp:Explorer:Default',
    //'fs' => 'SystemApp:Explorer:Fs',

    'weather' => 'Weather:Default',
    'weather' => 'Weather:Weather',
    'mail' => 'Mail:Default',
    'mailsettings' => 'Mail:Settings',
    'talker' => 'Talker:Skype',
    'television' => 'TV:Default',
    'news' => 'News:Default',
    'gallery' => 'Gallery:Default',
    'picasaAuthenticator' => 'Gallery:PicasaAuthenticator',
    'preferences' => 'ElderlyPreferences:Default',
    'application' => 'Application:Application',
    'people' => 'People:Default',
    'friends' => 'People:Friend',
));

// Step 4: Setup application router
$router = $application->getRouter();


//Needed?
$router[] = new Route('people/<presenter>/<action>[/<id>]', array(
            'module' => 'Applications:People',
            'presenter' => 'Default',
            'action' => 'default',
        ));

//$router[] = new Route('fs/<action>', array(
//    'module' => 'Webtop:SystemApp:Explorer',
//    'presenter' => 'fs',
//    'action' => 'default',
//));

$router[] = new Route('kernel/<presenter #kernel>/<action>[/<paginator-page \d+>][/<fullName>]', array(
            'module' => 'Kernel',
            'presenter' => 'Overview',
            'action' => 'default',
        ));

//$router[] = new Route('business[/<presenter #webtop>]', array(
//    'module' => 'Webtop',
//    'presenter' => 'Default',
//    'action' => 'business',
//));

$router[] = new Route('try/<action>', array(
            'module' => 'Web',
            'presenter' => 'Default',
            'action' => 'try',
        ));

$router[] = new Route('api/<presenter>/<action>[/<id \d+>]', array(
            'module' => 'Api',
            'presenter' => 'Rest',
            'action' => 'default',
            'id' => null,
        ));

$router[] = new Route('app/<presenter #applications>/<action>', array(
            'module' => 'Applications',
            'presenter' => 'Default',
            'action' => 'default',
        ));


$router[] = new Route('<presenter #webtop>/<action>', array(
            'module' => 'Webtop',
            'presenter' => 'Default',
            'action' => 'default',
        ));

// the module loader loads installed modules (runs init methods)
$moduleLoader = new ModuleLoader();
$moduleLoader->loadInstalled();
// Step 5: Run the application!
$application->run();